* 0. IMPORT MERGE EPFR DATA with: weekly flows and monthly country allocations
use "$datapath/__EPFRdata__4__Main__Results.dta", clear
qui drop if ca <= 0 | mi(ca) // drop zero, negative and missing shares

***************************************************************************
* 1. merge with return data
merge m:1 country datem using "$datapath/Prediction_Differential.dta", keepusing(r) nogenerate // keep equity return "r"
drop if mi(fundid)
drop if mi(r)
***************************************************************************

***************************************************************************
* 2. compute country shares on total investment not in cash
gen Wint = ca*fundsize/100 // ca is country share (including cash) in percent, fundsize is total assets under management at the end of the month
bys fundid datem : egen netaum = total(Wint) // total equity investment across the 135 countries
drop if mi(Wint)
gen netca = Wint / netaum // country share net of cash
***************************************************************************

***************************************************************************
* REMOVE COUNTRIES IN WHICH THE FUND INVESTS LESS THAN 2 PERCENT ON AVERAGE
***************************************************************************
* 3. average share by country
bys fundid country (datem) : egen meannetca = mean(netca)
drop if meannetca <= 0.02 // this is k_{i,n} mentioned in the paper
drop Wint netaum netca meannetca

***************************************************************************
* 4. keep 35 countries
run "$buildpath/0_countryselection" // 35 countries
compress // reduce size for memory efficiency
***************************************************************************

***************************************************************************
* 5. merge with econometrician expected excess returns
merge m:1 datem country  using "$datapath/Portfolio_Paper_Predictions.dta", nogenerate 
replace er_24d90 = 0 if country == "unitedstates" // that's because returns differentials w.r.t United States
drop if mi(fundid)
drop if mi(er_24d90)
***************************************************************************

***************************************************************************
* 6a. Country shares out of the 35 countries
gen Wint = ca*fundsize/100
bys fundid country (datem) : gen lWint = Wint[_n-1] // past equity investment in Millions of US dollars
bys fundid datem : egen W35it = total(Wint), m
bys fundid datem : egen lW35it = total(lWint), m

gen zint = Wint/W35it // country shares out of th 35 countries 
gen lzint = lWint/lW35it // past country share
***************************************************************************

***************************************************************************
* 6b. Average country share
bys fundid country (datem) : egen zbar = mean(zint) // average country share

* save average country share to compute the fund expected excess returns
preserve
	collapse zbar = zint, by(fundid country)
		save "$datapath/Funds_zbarCollapse.dta", replace
restore
***************************************************************************

sort fundid country datem

***************************************************************************
* 7. buy-and-hold portfolio + valuation effect
gen zbh = lzint*(1+r)/(1+monthlyportfoliochangepct) // monthlyportfoliochangepct is the portfolio return reported by funds to EPFR
gen val = zbh - lzint // valuation effect
bys fundid country (datem) : gen lval = val[_n-1] // past valuation effect
***************************************************************************

***************************************************************************
* 8. clean variables list for memory efficiency
drop if mi(lzint)
keep datem fundid country id monthlyportfoliochangepct fundsize ca a_region Wint W35it zint lzint r zbh val lval  zbar
compress
***************************************************************************

***************************************************************************
* Figure C1 Online Appendix - Distribution average country share
local figureC1 = 1
if `figureC1' == 1 {
	preserve
	collapse zbar, by(fundid country)

	sum zbar, d
	gen s95 = 1 if zbar < r(p95)

	twoway (kdensity zbar if s95 == 1, lcolor(black)), graphregion(color(white)) xtitle(" " "Average Country Share (out of the 36 Countries)") ytitle("Density" " ") title("") ylabel(0(3)15) plotregion(margin(none)) 
	graph export "$figpath/figureC1.pdf", replace 
	restore
}
***************************************************************************

***************************************************************************
* 9. RUN SCRIPTS TO COMPUTE ALL DATA CONCERNING FUNDS EXPECTED EXCESS RETURNS
local run = 1
if `run' == 1 {
	
	qui tempfile master
	qui save `master', replace
	
	* 0: Funds average z out of country n
	run "$finalpath/codes/_do_estim/Portfolio_Paper_zbar"
	
	* a: Funds Excess Return
	run "$finalpath/codes/_do_estim/Portfolio_Paper_Excessreturn"
	
	* b: instruments
	run "$finalpath/codes/_do_estim/Portfolio_Paper_Instruments"
	
	* c: RISK
	run "$finalpath/codes/_do_estim/Funds_Risk"
	
	* d: use past shares as weight to compute expected excess returns
	run "$finalpath/codes/_do_estim/Portfolio_Paper_Excessreturn_Weighting_Table_B1_Col_6"
	
	* e: confidence index variables 
	run "$finalpath/codes/_do_estim/Portfolio_Paper_Confidence_Table_B3"
	
	qui use `master', clear
}
***************************************************************************

***************************************************************************
* 10. RUN ALL DATA CONCERNING FUNDS EXPECTED EXCESS RETURNS WITH MAIN DATA
* a: Funds Excess Return
qui merge 1:1 fundid country datem using "$finalpath/dta/Portfolio_Paper_Excessreturn", nogenerate

* b: instruments
qui merge 1:1 fundid country datem using "$finalpath/dta/Portfolio_Paper_Instruments", nogenerate
drop if mi(fundid)

* c: risk
qui merge 1:1 fundid country datem using "$finalpath/dta/Funds_Risk", nogenerate
drop if mi(fundid)

* d: expected excess return using the past shares as weight. used in table B1 column 6. 
qui merge 1:1 fundid country datem using "$finalpath/dta/Portfolio_Paper_Excessreturn_Weighting_Table_B1_Col_6", nogenerate
drop if mi(fundid)

* e: confidence indicators used in table B3
qui merge 1:1 fundid country datem using "$finalpath/dta/Portfolio_Paper_Confidence_Table_B3.dta", nogenerate
drop if mi(fundid)

***************************************************************************
* COMPUTE AVERAGE SIGMA^2 IN THE PAPER: average variance excess return
preserve
	bys fundid country : egen sd = sd(z_r1) // standard deviation for each (i,n) pairs
	gen var = sd^2
	collapse var , by(fundid country)
		sum var
restore
***************************************************************************

***************************************************************************
* labels
gen er = .
gen xer = .
label var zint " $ z_{i,n,t} $ "
label var lzint " $ z_{i,n,t-1} $ "
label var val " $ val_{i,n,t} $ "
label var zbh " $ z^{bh}_{in,t} $ "
label var er " $ ER_{i,n,t} $ "
label var xer " $ ER_{i,n,t} $ "

***************************************************************************
* scale present discounted expected excess returns to get compareable coefficients 
foreach v of varlist z*_24*  {
	qui replace `v' = `v'/24
}
foreach v of varlist z*_12*  {
	qui replace `v' = `v'/12
}
foreach v of varlist z*_36*  {
	qui replace `v' = `v'/36
}
***************************************************************************

***************************************************************************
* PRESENT DISCOUNTED EXPECTED EXCESS RETURNS SCALED BY THE FACTOR AT WHICH FUND DISCOUNTS THE FUTURE (DELTA)
gen ER24d89 = z_er_24d89*(1-0.89)
gen ER24d90 = z_er_24d90*(1-0.9)
gen ER24d92 = z_er_24d92*(1-0.92)
gen ER24d925 = z_er_24d925*(1-0.925)
gen ER24d93 = z_er_24d93*(1-0.93)
gen ER24d94 = z_er_24d94*(1-0.94)

gen ER12d90 = z_er_12d90*(1-0.90)
gen ERNONREC24d94 = z_er_NONREC_24d94*(1-0.94)
gen ERweight24d94 = zint_er_24d94*(1-0.94)
gen ER36d93 = z_er_36d93*(1-0.93)
***************************************************************************

***************************************************************************
* PARAMETERS TO COMPUTE IMPUSE RESPONSE FUNCTION FIGURE 1 in MAIN TEXT
local figure1 = 0
if `figure1' == 1 {
* persistence
	preserve

		replace z_er_24d925 = z_er_24d925*24 // unscale fund expected excess returns
		bys fundid country (datem) : gen lz_er_24d925 = z_er_24d925[_n-1]
		bys fundid country (datem) : gen lER24d925 = ER24d925[_n-1]
		bys fundid country (datem) : gen lz_er_1 = z_er_1[_n-1]
		
		reghdfe z_er_1 lz_er_1, absorb(i.fundid##i.id, savefe) resid vce(cluster datem)
		predict res1, resid
		
		reghdfe ER24d925 lER24d925, absorb(i.fundid##i.id, savefe) resid vce(cluster datem)
		
		predict res24, resid
		
		reghdfe z_er_24d925 lz_er_24d925, absorb(i.fundid##i.id, savefe) resid vce(cluster datem)
		
		predict res24bis, resid
		
		collapse (sd) res1 res24 res24bis, by(fundid country)
		
		sum res1 res24 res24bis
	restore

}
***************************************************************************

***************************************************************************
* DEFINE NEW VARIABLES FOR THE INSTRUMENTS
cap drop q*
gen q1 = z_dD // first difference (log) dividends
gen q2 = z_E // (log) earnings
gen q3 = lval // lag valuation effect
gen q4 = z_dE // first difference (log) earnings
gen q5 = z_Y // (log) industrial production
gen q6 = z_B // (log) book value
gen q7 = z_H // (log) bond index
gen q8 = z_dH // first difference (log) bond index
gen q9 = z_D // (log) dividends
gen q10 = z_dY // first difference (log) industrial production
gen q11 = z_dB // first differnece (log) book value
gen q12 = z_I // 3-months interest rates
gen q13 = z_dI // first difference 3-months interest rates
***************************************************************************

***************************************************************************
* DEMEAN DEPENDANT, INDEPENDANT AND INTRUMENTAL VARIABLES TO REMOVE FUND-COUNTRY FIXED EFFECT
cap drop *_hat
cap drop *_res
foreach v of varlist zint lzint val ER24d90 q* {
	qui reghdfe `v' , absorb(fundid#id, savefe) resid
	qui predict `v'_hat, xbd
	qui gen `v'_res = `v' - `v'_hat
}
***************************************************************************

***************************************************************************
* FIRST STAGE REGRESSIONS

* labels
label var q1 " $ \Delta d_{i,n,t} $ "
label var q2 " $ e_{i,n,t} $ "
label var q3 " $ val_{i,n,t-1} $ "
label var q4 " $ \Delta e_{i,n,t} $ "
label var q5 " $ y_{i,n,t} $ "
label var q6 " $ b_{i,n,t} $ "
label var q7 " $ h_{i,n,t} $ "
label var q8 " $ \Delta h_{i,n,t} $ "
label var q9 " $ d_{i,n,t} $ "
label var q10 " $ \Delta y_{i,n,t} $ "
label var q11 " $ \Delta b_{i,n,t} $ "
label var q12 " $ i_{i,n,t} $ "
label var q13 " $ \Delta i_{i,n,t} $ "

label var q1_res " $ \Delta d_{i,n,t} $ "
label var q2_res " $ e_{i,n,t} $ "
label var q3_res " $ val_{i,n,t-1} $ "
label var q4_res " $ \Delta e_{i,n,t} $ "
label var q5_res " $ y_{i,n,t} $ "
label var q6_res " $ b_{i,n,t} $ "
label var q7_res " $ h_{i,n,t} $ "
label var q8_res " $ \Delta h_{i,n,t} $ "
label var q9_res " $ d_{i,n,t} $ "
label var q10_res " $ \Delta y_{i,n,t} $ "
label var q11_res " $ \Delta b_{i,n,t} $ "
label var q12_res " $ i_{i,n,t} $ "
label var q13_res " $ \Delta i_{i,n,t} $ "

* rescale variables to have scaled coefficients
gen val_res1000 = val_res*1000
gen ER24d90_res1000 = ER24d90_res*1000
***************************************************************************

***************************************************************************
* table 3 - first stage main results
gen risk1000 = 1000*risk
gen frisk1000 = 1000*frisk
gen lrisk1000 = 1000 * lrisk
label var risk " $ risk_{i,n,t} $ "
label var frisk " $ risk_{i,n,t+1} $ "
label var lrisk " $ risk_{i,n,t-1} $ "
label var risk1000 " $ risk_{i,n,t} $ "
label var frisk1000 " $ risk_{i,n,t+1} $ "
label var lrisk1000 " $ risk_{i,n,t-1} $ "

est clear

* column 1
reghdfe lzint_res q1_res q2_res q3_res q4_res q5_res q6_res q7_res q8_res , noabsorb cluster(datem)
est sto reg1

* column 2
reghdfe val_res1000 q1_res q2_res q3_res q4_res q5_res q6_res q7_res q8_res , noabsorb cluster(datem)
est sto reg2

* column 3
reghdfe ER24d90_res1000 q1_res q2_res q3_res q4_res q5_res q6_res q7_res q8_res , noabsorb cluster(datem)
est sto reg3

* column 4
reghdfe frisk1000 risk1000 q1 q2 q3 q4 q5 q6 q7 q8, absorb(fundid#id, savefe) resid cluster(datem)
est sto reg4
***************************************************************************

***************************************************************************
* predict risk for table 4 column (4)
cap drop frisk*_hat

reghdfe frisk1000 risk1000 q9 q12 , absorb(fundid#id, savefe) resid cluster(datem)
predict frisk_hat, xbd
gen frisk1000_hat = frisk_hat/1000
label var frisk_hat " $ \widehat{risk}_{i,n,t+1} $ "
label var frisk1000_hat " $ \widehat{risk}_{i,n,t+1} $ "
***************************************************************************

***************************************************************************
* Table 4 - Benchmark

est clear
* column 1
replace xer = ER24d90
reghdfe zint lzint val xer, absorb(fundid#id) vce(cluster datem)
matrix f = e(first)
est sto reg1

** column 2
replace xer = ER24d92
ivreghdfe zint (lzint val xer = q1 q2 q3 q4 q5 q6 q7 q8 ), absorb(fundid#id) cluster(datem) first
matrix f = e(first)
est sto reg2

** column 3
replace xer = ER24d92
ivreghdfe zint (lzint zbh xer = q1 q2 q3 q4 q5 q6 q7 q8 ), absorb(fundid#id) cluster(datem) first
matrix f = e(first)
est sto reg3

** column 4
replace xer = ER24d92
ivreghdfe zint (lzint val xer = q1 q2 q3 q4 q5 q6 q7 q8 ) frisk1000_hat, absorb(fundid#id) cluster(datem) first
matrix f = e(first)
est sto reg4

** column 5
replace xer = ER24d925
ivreghdfe zint (lzint val xer = q1 q2 q3 q4 q5 q6 q7 q8 ) frisk, absorb(fundid#id) cluster(datem) first
matrix f = e(first)
est sto reg5
***************************************************************************

***************************************************************************
* table 6 - first stage main robustness
est clear

* column 1
reghdfe lzint_res q1_res q2_res q3_res q5_res  , noabsorb cluster(datem)
est sto reg1

* column 2
reghdfe val_res1000 q1_res q2_res q3_res q5_res , noabsorb cluster(datem)
est sto reg2

* column 3
reghdfe ER24d90_res1000 q1_res q2_res q3_res q5_res  , noabsorb cluster(datem)
est sto reg3

* column 4
reghdfe lzint_res q4_res q6_res q7_res q8_res , noabsorb cluster(datem)
est sto reg4

* column 5
reghdfe val_res1000  q4_res q6_res q7_res q8_res , noabsorb cluster(datem)
est sto reg5

* column 6
reghdfe ER24d90_res1000 q4_res q6_res q7_res q8_res , noabsorb cluster(datem)
est sto reg6
***************************************************************************



***************************************************************************
* Table 7 - Robustness Portfolio - First three columns
est clear

** benchmark - IV
replace xer = ER24d92
ivreghdfe zint (lzint val xer = q1 q2 q3 q4 q5 q6 q7 q8 ), absorb(fundid#id) cluster(datem) first
matrix f = e(first)
est sto reg1

** benchmark - IV
replace xer = ER24d92
ivreghdfe zint (lzint val xer = q1 q2 q3 q5 ), absorb(fundid#id) cluster(datem) first
matrix f = e(first)
est sto reg2

** benchmark - IV
replace xer = ER24d925
ivreghdfe zint (lzint val xer = q4 q6 q7 q8 ), absorb(fundid#id) cluster(datem) first
matrix f = e(first)
est sto reg3
***************************************************************************

***************************************************************************
*** HETEROGENEITY + INTERACTIONS
cap drop zbar_*
foreach v of varlist ER* q1 q2 q3 q5{
	qui gen zbar_`v' = zbar * `v'
}
gen ln_zint = ln(zint)
gen ln_lzint = ln(lzint)

label var zbar_ER24d92 " $ \bar{z}_{in} \times ER_{i,n,t} $ "
label var ER24d92 " $ ER_{i,n,t} $ "
label var ln_lzint " $ \ln z_{i,n,t-1} $ "
label var ln_zint " $ \ln z_{i,n,t} $ "
***************************************************************************

***************************************************************************
* Table 7, column 4

est clear
* column 4
ivreghdfe zint (lzint val ER24d92 zbar_ER24d92 = q1 q2 q3 q5 zbar_q1 zbar_q2 zbar_q3 zbar_q5 ), absorb(id#fundid) cluster(datem) first
matrix f = e(first)
est sto reg4
***************************************************************************

***************************************************************************
*** TABLE B1 - ALTERNATIVES SPECIFICATIONS

est clear

* column 1
replace xer = ER24d89
reghdfe zint lzint val xer , absorb(fundid#id id#datem) cluster(datem) 
est sto reg1

*** column 2 - 12-month horizons
replace xer = ER12d90
ivreghdfe zint (lzint val xer = q1 q2 q3 q4 q5 q6 q7 q8), absorb(fundid#id) cluster(datem) first
matrix f = e(first)

est sto reg2

***  column 3 - 36-month horizons
replace xer = ER36d93
ivreghdfe zint (lzint val xer = q1 q2 q3 q4 q5 q6 q7 q8), absorb(fundid#id) cluster(datem) first
matrix f = e(first)
est sto reg3

*** column 4 - beta 0.96
replace xer = ER24d92
ivreghdfe zint (lzint val xer = q1 q2 q3 q4 q5 q6 q7 q8), absorb(fundid#id) cluster(datem) first
matrix f = e(first)
est sto reg4

*** column 5 - beta 0.98
replace xer = ER24d94
ivreghdfe zint (lzint val xer = q1 q2 q3 q4 q5 q6 q7 q8), absorb(fundid#id) cluster(datem) first
matrix f = e(first)
est sto reg5


**** column 6 - weighting
replace xer = ERweight24d94
ivreghdfe zint (lzint val xer = q1 q2 q3 q4 q5 q6 q7 q8), absorb(fundid#id) cluster(datem) first
matrix f = e(first)
est sto reg6

**** column 7 - non-recursive
replace xer = ERNONREC24d94
ivreghdfe zint (lzint val xer = q1 q2 q3 q4 q5 q6 q7 q8 ), absorb(fundid#id) cluster(datem) first
matrix f = e(first)
est sto reg7
***************************************************************************

***************************************************************************
*** TABLE B2 - ALTERNATIVES SAMPLES
est clear

*** column 1 - from Jan 2012
replace xer = ER24d92
ivreghdfe zint (lzint val xer = q1 q2 q3 q4 q5 q6 q7 q8) if datem >= ym(2012,1), absorb(fundid#id) cluster(datem) first
matrix f = e(first)
est sto reg1

**** column 2 - more than 24 months 
cap drop nt
bys fundid country: egen nt = count(datem)
replace xer = ER24d93
ivreghdfe zint (lzint val xer = q1 q2 q3 q4 q5 q6 q7 q8) if nt >= 24, absorb(fundid#id) cluster(datem) first
matrix f = e(first)
est sto reg2

**** column 3
* to obtain rerun column 3, change the threshold parameter "k_{i,n}" at beginning of file
***************************************************************************

***************************************************************************
*** TABLE B3 - CONFIDENCE INDEX
cap drop z_*10000
gen z_cci10000 = z_cci /10000
gen z_bci10000 = z_bci /10000
gen z_cli10000 = z_cli /10000

label var z_cci10000 "Consumer Conf. Index"
label var z_bci10000 "Business Conf. Index"
label var z_cli10000 "Leading Indicators"

est clear

* column 1 more than Jan 2010
replace xer = ER24d93
ivreghdfe zint (lzint val xer = q1 q2 q3 q4 q5 q6 q7 q8 ) if !mi(z_cci10000), absorb(fundid#id) cluster(datem) first
matrix f = e(first)
est sto reg1

*** column 2
replace xer = ER24d92
ivreghdfe zint (lzint val xer = q1 q2 q3 q4 q5 q6 q7 q8 z_cci) , absorb(fundid#id) cluster(datem) first
matrix f = e(first)
est sto reg2

**** column 3 
cap drop nt
bys fundid country: egen nt = count(datem)
replace xer = ER24d93
ivreghdfe zint (lzint val xer = q1 q2 q3 q4 q5 q6 q7 q8 z_bci) , absorb(fundid#id) cluster(datem) first
matrix f = e(first)
est sto reg3

* column 4
replace xer = ER24d93
ivreghdfe zint (lzint val xer = q1 q2 q3 q4 q5 q6 q7 q8 z_cli) , absorb(fundid#id) cluster(datem) first
matrix f = e(first)
local reg4_SWFval = round(f[8,2],.01)
local reg4_SWFz = round(f[8,1],.01)
local reg4_SWFer = round(f[8,3],.01)

est sto reg4
***************************************************************************

***************************************************************************
* TABLE B4 - LOG PORTFOLIO REGRESSIONS
ivreghdfe ln_zint (ln_lzint val ER24d92 = q1 q2 q3 q4 q5 q6 q7 q8), absorb(id#fundid) cluster(datem) first

est clear
matrix f = e(first)
est sto reg1
***************************************************************************